import {  ErrorBlock, Toast } from 'antd-mobile';
import { getMessage, TextWidget } from '@inbiz/react';
import BizBody from '@inbiz/basic/BizBody';
import BizHeader from '@inbiz/basic/BizHeader';
import { InbizIcon, useInbizRouter } from '@inbiz/utils';
import { routerPrefix } from 'InbizConfig';
import List from './view/list';
import Detail from './view/details';
import Stream from './view/streamList';
import DetailHeader from './view/detailHeader';
import Card from './view/Card';
import CardStream from './view/CardStream';
import Summary from './view/Summary';
import SummaryStream from './view/SummaryStream';
import { addCounts } from '../../service';
import './style.less';
import { useState } from 'react';


export interface NewsProps {
  showCate: any,
  loadNumber: any,
  newsKey: any,
  fixNewKey:any,
  standOut: any,
  goMore: any,
  goToDetail: any,
  seeMore: any,
  dataLoadWap: any,
  showPositionWap: any,
  showTextWap: any,
  everytimeLoad: any;
  // data:any;
  // setData:any;
  emit: () => void,
  callBackEvents: {
    onParams: <T>(data: T) => Promise<T>,
    onRequest: <T> (data: object) => Promise<any>,
    onData: <T>(data: T) => Promise<T>
  }
}

const News = (props: any) => {
  const {newsKey}=props
  const fixNewKey = Array.isArray(newsKey) ? newsKey :newsKey? [newsKey]:[];
  const history = useInbizRouter();
  if (props.viewTypeWap === 'detail') {
    props.style.padding = props.dedailPaddingWap;
  }
  const [data, setData] = useState<any[]>([]);

  // 查看更多跳转
  const goMore = () => {
    if (!props.correlationPageWap) {
      Toast.show(getMessage('news.tips.message_more_none'));
      return;
    }
    const [name] = props.correlationPageWap.split(',');
    let url = `${routerPrefix}${name}`;
    history.push(url);
  };

  //详情页跳转
  const goToDetail = (record: any, cateId?: string) => {
    const { ID, ContentUrl } = record;
    if (props.detail || ContentUrl) {
      addCounts({ entityld: ID }).then(() => { }).catch(() => { });
    };
    if (!!ContentUrl) {
      window.open(ContentUrl);
      return;
    };
    if (!props.detail) return;
    const [name] = props.detail.split(',');
    let url = `${routerPrefix}${name}?newsId=${ID}&cateId=${cateId || record?.ContentCategory}`;
    history.push(url);
  };

  return (
    <div className="news">
      {(props.viewTypeWap == 'static' || props.viewTypeWap == 'bigCard' || props.viewTypeWap == 'summary') && (
        <BizHeader
          {...props}
          extra={
            props.seeMore &&
              props.dataLoadWap === 'more' &&
              props.showPositionWap === 'right-top' ? (
              <div
                onClick={goMore}
                style={{
                  padding: 0,
                  fontSize: '12px',
                  color: 'var(--inbiz-color-text)',
                }}
              >
                <TextWidget>{props.showTextWap || ''}</TextWidget>&nbsp;{'>'}
              </div>
            ) : null
          }
        />
      )}
      {props.viewTypeWap === 'detail' && <DetailHeader {...props} />}
      <BizBody {...props}>


          <>
          {props.viewTypeWap === 'detail' && (
          <Detail fixNewKey={fixNewKey} {...props} goToDetail={goToDetail} />
        )}
            {props.viewTypeWap === 'static' && props.dataLoadWap === 'more' && (
              <List fixNewKey={fixNewKey} {...props} goToDetail={goToDetail} />
            )}
            {props.viewTypeWap === 'static' && props.dataLoadWap === 'streamLoad' && (
              <Stream fixNewKey={fixNewKey} {...props} goToDetail={goToDetail} />
            )}
            {props.viewTypeWap === 'bigCard' && props.dataLoadWap === 'more' && (
              <Card fixNewKey={fixNewKey} data={data} setData={setData}{...props} goMore={goMore} goToDetail={goToDetail} />
            )}
            {props.viewTypeWap === 'bigCard' && props.dataLoadWap === 'streamLoad' && (
              <CardStream fixNewKey={fixNewKey} {...props} goToDetail={goToDetail} />
            )}
            {props.viewTypeWap === 'summary' && props.dataLoadWap === 'more' && (
              <Summary fixNewKey={fixNewKey} {...props} goMore={goMore} goToDetail={goToDetail} />
            )}
            {props.viewTypeWap === 'summary' && props.dataLoadWap === 'streamLoad' && (
              <SummaryStream fixNewKey={fixNewKey} {...props} goToDetail={goToDetail} />
            )}
          </>
         



      </BizBody>
    </div>
  );
};

export default News;
